package com.gs.demo.controller;

import com.gs.demo.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


public class BaseController implements MessageSourceAware {
    HttpSession mySession = null;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @ModelAttribute
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        try {
            request.setCharacterEncoding("utf-8");
            mySession = request.getSession();
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }
    }

    @ModelAttribute
    public void setResponse(HttpServletResponse response) {
        this.response = response;
        response.setCharacterEncoding("utf-8");
    }


    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request, HttpServletRequest requests, HttpServletResponse response) {
        mySession = requests.getSession();
        // 转换日期
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // 去除空字符串
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    protected String getSuccessResult(String... message) {
        return ResultUtil.getSuccessResult(message);
    }

    protected String getSuccessResultMap(Map<String, Object> data, String... message) {
        return ResultUtil.getSuccessResultMap(data, message);
    }

    protected String getSuccessResultKey(String key, Object value, String... message) {
        return ResultUtil.getSuccessResultKey(key, value, message);
    }

    protected String getErrorResult(Integer code, String message) {
        return ResultUtil.getErrorResult(code, message);
    }

    @Autowired
    private MessageSource messageSource;

    /* 获取国际化信息
     * @param messageCode
     * @param params
     * @param defaultValue
     * @return
     */
    protected String getMessage(String messageCode, Object[] params,
                                String defaultValue) {
        return (messageSource.getMessage(messageCode, params, defaultValue,
                ((ServletRequestAttributes) RequestContextHolder
                        .getRequestAttributes()).getRequest().getLocale()));
    }

    @Override
    public void setMessageSource(MessageSource ms) {
        this.messageSource = ms;
    }
}
